﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;
using System.IO;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Web.Security;

public partial class admin_EditProfile : SitePage
{

    #region Init,Load
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitData();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadData();
    }
    #endregion

    #region Methods

    private void LoadData()
    {
        MembershipUser user = Membership.GetUser(this.userloginId);
        if (user != null)
        {
            ProfileCommon profile = this.Profile.GetProfile(user.UserName);
            txtFirstName.Text = profile.FirstName;
            txtLastname.Text = profile.LastName;
            txtEmail.Text = user.Email;
            txtUserName.Text = user.UserName;
        }
    }

    private void InitData()
    {
        string validateGroup = "user";
        vsumUser.ValidationGroup = btnSave.ValidationGroup = reqFirstName.ValidationGroup = reqLastName.ValidationGroup = reqEmail.ValidationGroup = regvEmail.ValidationGroup = validateGroup;
        //Init message
        reqFirstName.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "First name");
        reqLastName.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "Last name");
        reqEmail.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "Email");
        regvEmail.ErrorMessage = string.Format(Resources.Resource.MSG_VALID, "Email");
    }
    private void UpdateUser()
    {
        try
        {
            MembershipUser user = Membership.GetUser(txtUserName.Text);
            //check email is exist
            string userNameCheck = Membership.GetUserNameByEmail(txtEmail.Text);
            if (userNameCheck != null && !userNameCheck.Equals(txtUserName.Text))
            {
                lblErrorMessage.Text = string.Format(Resources.Resource.MSG_CHECK_EXISTS, "Email");//Email is exist
                return;
            }
            user.Email = txtEmail.Text;
            Membership.UpdateUser(user);
            ProfileCommon profile = this.Profile.GetProfile(user.UserName);
            profile.FirstName = txtFirstName.Text;
            profile.LastName = txtLastname.Text;
            profile.Save();
            lblErrorMessage.Text = string.Empty;

        }
        catch (MembershipCreateUserException e)
        {
            lblErrorMessage.Text = Resources.Resource.MSG_UPDATE_ERROR;
        }
    }
    #endregion

    #region Events, Handle
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            UpdateUser();
        }
        else
        {
            vsumUser.ShowMessageBox = false;
            vsumUser.ShowSummary = true;
        }
    }

    #endregion


}